//以下代码只适合 浏览器, 不适合 nodejs和android 因为没有 window.btoa blob对象


//字符串转base64
export function str_b64(str) {
	// 对字符串进行编码
	var encode = encodeURI(str);
	// 对编码的字符串转化base64
	var base64 = btoa(encode);
	return base64;
}

// base64转字符串
export function b64_str(base64) {
	// 对base64转编码
	var decode = atob(base64);
	// 编码转字符串
	var str = decodeURI(decode);
	return str;
}


export function buffer_str1(buffer) {
	return String.fromCharCode(...new Uint8Array(buffer));
}

export function buffer_str(buffer) {
	var binary = '';
	var bytes = new Uint8Array(buffer);
	var len = bytes.byteLength;
	for (var i = 0; i < len; i++) {
		binary += String.fromCharCode(bytes[i]);
	}
	return binary
}

export function str_buffer(rawData) {
	let outputArray = new Uint8Array(rawData.length);
	for (var i = 0; i < rawData.length; ++i) {
		outputArray[i] = rawData.charCodeAt(i);
	}
	return outputArray;
}
export function buffer_blob(buffer) {
	var int8View = new Uint8Array(buffer);
	var blob = new Blob([int8View]);
	return blob;
}

/**
 * 先把 arraybuffer 转成 blob  new Blob([arraybuffer],'')
 * 再把blob 转成 url objectURL = URL.createObjectURL(blob);
 * 图片应该就可以读取
 */
export function buffer_url(buffer) {
	let blob = new Blob([buffer], '')
	return URL.createObjectURL(blob);
}

export function buffer_b64(buffer) {
	let binary = buffer_str1(buffer)
	return window.btoa(binary);
}
//图片转base64
export function buffer_b64url(buffer, 图像格式) {
	const str = String.fromCharCode(...new Uint8Array(buffer));
	图像格式 = 图像格式 || 'image/jpeg';
	return `data:${图像格式};base64,${str}`; //反引号(`)标识是ES6新增的模板字符串
}
export function b64url_u8Arr2(dataurl) {
	const padding = '='.repeat((4 - base64String.length % 4) % 4);
	const base64 = (base64String + padding)
		.replace(/\-/g, '+')
		.replace(/_/g, '/');
	const rawData = window.atob(base64);
	const outputArray = new Uint8Array(rawData.length);
	for (let i = 0; i < rawData.length; ++i) {
		outputArray[i] = rawData.charCodeAt(i);
	}
	return outputArray;
}
export function b64url_u8arr(dataurl) { 
	let obj = {};
	let arr = dataurl.split(','),
		bstr = atob(arr[1]),
		n = bstr.length;
	obj.mime = arr[0].match(/:(.*?);/)[1];
	obj.u8arr = new Uint8Array(n);
	while (n--) {
		obj.u8arr[n] = bstr.charCodeAt(n);
	}
	return obj;
}

export function b64url_blob(base64) {
	let ia = b64url_u8arr(base64);
	return new Blob([ia.u8arr], {
		type: ia.mime
	});
}

export function b64url_file(dataurl, filename) { //将base64转换为文件
	let ia = b64url_u8arr(base64);
	return new File([ia.u8arr], filename, {
		type: ia.mime
	});
}

export function blob_u8arr(blob, call) {
	const reader = new FileReader();
	reader.onload = function() {
		let u8 = new Uint8Array(this.result)
		call(u8);
	}
	reader.readAsArrayBuffer(blob);
}
export async function blob_u8arr2(blob) {
	let val = null;
	await blob.arrayBuffer().then(v => {
		val = new Uint8Array(v)
	})
	return val;
}

export function blob_b64url(blob, callback) {
	const a = new FileReader();
	a.onload = function(e) {
		callback(e.target.result);
	}
	a.readAsDataURL(blob);
}
/**
 * 超大文本流转 String,回调方式读取
 * @param {Uint8Array} uint8arr 
 * @param {String} callback 
 */
export function largeuint8Arr_Str(uint8arr, callback) {
    var bb = new Blob([uint8arr]);
    var f = new FileReader();
    f.onload = function (e) {
        callback(e.target);
    };
    f.readAsText(bb);
}